import app from '@/app'
import Factory from './factory'
import { totalGroup } from '@/api/store/order_pay'
import payStore from '@/plugin/config/store/pay'

class PayFactory extends Factory {

  async before() {
    this.pay = (await payStore.get()).slice(0, 3).map(({ id, name }) => {
      return { id, name }
    })

    if(!this.pay) {
      throw Error()
    }
  }

  async getData(start, end, data) {
    const res = await totalGroup({ time: [start.unix(), end.unix()] })

    // 因为有多线程，就直接填充
    const total = { m: 0, c: 0 }
    res.forEach(item => {
      // 存在前三支付方式中，如果不存在就累加到 0 中
      if(this.pay.findIndex(pay => pay.id == item.pay_id) > -1) {
        data[item.pay_id] = {
          c: item.count,
          m: item.money
        }
      } else {
        data[0] = {
          c: app.math.add(data[0]?.c ?? 0, item.count),
          m: app.math.add(data[0]?.m ?? 0, item.money)
        }
      }

      // 累加一个小计
      total.c = app.math.add(total.c, item.count)
      total.m = app.math.add(total.m, item.money)
    })

    if(total.c) {
      data.total = total
    }
  }

  // 处理统计
  total() {
    // 要循环的字段
    const fields = [...this.pay.map(pay => pay.id), 0, 'total']

    // 循环天
    this.day.forEach(data => {
      // 循环支付的字段
      fields.forEach(field => {
        const value = data[field]
        if(value) { 
          // 统计年
          this.year[field] = {
            m: app.math.add(this.year[field]?.m ?? 0, value.m),
            c: app.math.add(this.year[field]?.c ?? 0, value.c),
          }
          // 统计季度 
          this.quarter[data.id[0]][field] = {
            m: app.math.add(this.quarter[data.id[0]][field]?.m ?? 0, value.m),
            c: app.math.add(this.quarter[data.id[0]][field]?.c ?? 0, value.c),
          }
          // 统计月
          this.month[data.id[1]][field] = {
            m: app.math.add(this.month[data.id[1]][field]?.m ?? 0, value.m),
            c: app.math.add(this.month[data.id[1]][field]?.c ?? 0, value.c),
          }
        }
      })
    })

    // 记录支付数据
    this.year.pay = this.pay
  }

  // 查询统计数据
  getTotal(type, index, day) {
    const pay = this.year.pay
    return {
      year: () => {
        return {
          pay,
          data: this.year,
          list: this.month
        }
      },
      quarter: () => {
        const list = this.getDayList({ quarter: index })
        return {
          pay,
          data: this.quarter[index],
          list,
          time: list.map(item => {
            return this.getDayTime(item).dateSmall()
          })
        }
      },
      month: () => {
        const list = this.getDayList({ month: index })
        return {
          pay,
          data: this.month[index],
          list: list,
          time: list.map(item => {
            return this.getDayTime(item).dateSmall()
          })
        }
      },
      day: () => {
        const data = this.getDay(day.month(), day.day())
        return {
          pay,
          data,
          list: [data],
          time: [this.getDayTime(data).dateSmall()]
        }
      }
    }[type]()
  }
}

export default PayFactory